package com.ruoyi.job.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.job.domain.EtlScriptInfo;
import com.ruoyi.job.service.IEtlScriptInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * Kettle脚本Controller
 * 
 * @author ruoyi
 * @date 2021-07-31
 */
@RestController
@RequestMapping("/taskScript")
public class EtlScriptInfoController extends BaseController
{
    @Autowired
    private IEtlScriptInfoService etlScriptInfoService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询Kettle脚本列表
     */
    @PreAuthorize(hasPermi = "job:taskScript:list")
    @GetMapping("/list")
    public TableDataInfo list(EtlScriptInfo etlScriptInfo)
    {
        startPage();
        List<EtlScriptInfo> list = etlScriptInfoService.selectEtlScriptInfoList(etlScriptInfo);
        return getDataTable(list);
    }

    /**
     * 导出Kettle脚本列表
     */
    @PreAuthorize(hasPermi = "job:taskScript:export")
    @Log(title = "Kettle脚本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EtlScriptInfo etlScriptInfo) throws IOException
    {
        List<EtlScriptInfo> list = etlScriptInfoService.selectEtlScriptInfoList(etlScriptInfo);
        ExcelUtil<EtlScriptInfo> util = new ExcelUtil<EtlScriptInfo>(EtlScriptInfo.class);
        util.exportExcel(response, list, "Kettle脚本数据");
    }

    /**
     * 获取Kettle脚本详细信息
     */
    @PreAuthorize(hasPermi = "job:taskScript:query")
    @GetMapping(value = "/{scriptId}")
    public AjaxResult getInfo(@PathVariable("scriptId") String scriptId)
    {
        return AjaxResult.success(etlScriptInfoService.selectEtlScriptInfoById(scriptId));
    }

    /**
     * 新增Kettle脚本
     */
    @PreAuthorize(hasPermi = "job:taskScript:add")
    @Log(title = "Kettle脚本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EtlScriptInfo etlScriptInfo)
    {
        return toAjax(etlScriptInfoService.insertEtlScriptInfo(etlScriptInfo));
    }

    /**
     * 修改Kettle脚本
     */
    @PreAuthorize(hasPermi = "job:taskScript:edit")
    @Log(title = "Kettle脚本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EtlScriptInfo etlScriptInfo)
    {
        return toAjax(etlScriptInfoService.updateEtlScriptInfo(etlScriptInfo));
    }

    /**
     * 删除Kettle脚本
     */
    @PreAuthorize(hasPermi = "job:taskScript:remove")
    @Log(title = "Kettle脚本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scriptIds}")
    public AjaxResult remove(@PathVariable String[] scriptIds)
    {
        return toAjax(etlScriptInfoService.deleteEtlScriptInfoByIds(scriptIds));
    }

    /**
     * 上传kettle脚本
     */
    @CrossOrigin
    @Log(title = "上传kettle脚本", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadKettleFile")
    public AjaxResult uploadKettleFile(@RequestParam("file") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            R<SysFile> fileResult = remoteFileService.uploadKettleFile(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return AjaxResult.error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();
            String pathUrl = fileResult.getData().getName();
//            if (userService.updateUserAvatar(loginUser.getUsername(), url))
//            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", url);
                ajax.put("pathUrl", pathUrl);
                // 更新缓存用户头像
//                loginUser.getSysUser().setAvatar(url);
//                tokenService.setLoginUser(loginUser);
                return ajax;
//            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 查询Kettle脚本列表
     */
    @PreAuthorize(hasPermi = "job:taskScript:getTaskSchedBySeq")
    @GetMapping("/getTaskSchedBySeq")
    public TableDataInfo getTaskSchedBySeq(EtlScriptInfo etlScriptInfo)
    {
        startPage();
        List<EtlScriptInfo> list = etlScriptInfoService.getTaskSchedBySeq(etlScriptInfo);
        return getDataTable(list);
    }

}
